//
//  CustomPickerView.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/16.
//

import UIKit

typealias BlockSelectContent = (_ title: String, _ row: NSInteger)->Void

class CustomPickerView: UIView {
    var dataArray:[String] = []
    var bgView: UIView!
    var cancelBtn: UIButton!
    var titleLab: UILabel!
    var sureButton: UIButton!
    var pickerView: UIPickerView!
    var contentHeight: CGFloat = 304 + IphoneXMarginBottom
    var selectContentBlock: BlockSelectContent!
    var content = ""
    var row = 0
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.init(hex: 0x000000, alpha: 0)
        let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
        tap.delegate = self
        addGestureRecognizer(tap)

        self.initPickerView()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initPickerView(){
        
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = .white
        self.addSubview(self.bgView)
        
        self.cancelBtn = UIButton.init()
        self.cancelBtn.setTitle(MMLocalizedString("cancel", comment: "取消"), for: .normal)
        self.cancelBtn.setTitle(MMLocalizedString("cancel", comment: "取消"), for: .highlighted)
        self.cancelBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
        self.cancelBtn.setTitleColor(Theme.mainColor, for: .normal)
        self.cancelBtn.setTitleColor(Theme.mainColor, for: .highlighted)
        self.cancelBtn.addTarget(self, action: #selector(cancelBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.cancelBtn)
        
        
        self.titleLab = UILabel.init()
        self.titleLab.backgroundColor = UIColor.clear
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.titleLab.text = MMLocalizedString("price_range", comment: "价格")
        self.titleLab.textAlignment = .center
        self.bgView.addSubview(self.titleLab)
        
        
        self.sureButton = UIButton.init()
        self.sureButton.setTitle(MMLocalizedString("confirm", comment: "确定"), for: .normal)
        self.sureButton.setTitle(MMLocalizedString("confirm", comment: "确定"), for: .highlighted)
        self.sureButton.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.sureButton.setTitleColor(Theme.mainColor, for: .normal)
        self.sureButton.setTitleColor(Theme.mainColor, for: .highlighted)
        sureButton.addTarget(self, action: #selector(onSureButtonAction), for: .touchUpInside)
        self.bgView.addSubview(self.sureButton)
                
        self.pickerView = UIPickerView(frame: CGRect(x: 0, y: kScreenHeight - 266, width: kScreenWidth, height: 266))
        self.pickerView.backgroundColor = .white
        self.pickerView.delegate = self
        self.pickerView.dataSource = self
        self.bgView.addSubview(self.pickerView)
        self.addSnpLayout()
        
    }
    
    ///布局
    func addSnpLayout() {
        
        self.bgView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(contentHeight)
            make.bottom.equalTo(contentHeight)
        }
        
        self.cancelBtn.titleLabel?.sizeToFit()
        let tempW1 = self.cancelBtn.titleLabel?.frame.size.width ?? 0
        self.cancelBtn.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.size.equalTo(CGSize.init(width: tempW1 + 10, height: 56))
        }
        
        self.sureButton.titleLabel?.sizeToFit()
        let tempW = self.sureButton.titleLabel?.frame.size.width ?? 0
        self.sureButton.snp.makeConstraints { (make) in
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.size.equalTo(CGSize.init(width: tempW + 10, height: 56))
        }
        
        self.titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(56)
        }
        
        self.pickerView.snp.makeConstraints { (make) in
            make.width.equalTo(kScreenWidth)
            make.top.equalTo(56)
            make.height.equalTo(250)
            make.centerX.equalTo(self.bgView.snp.centerX)
        }
    }
    
    func show() {
       
        self.bgView.snp.updateConstraints { (make) in
            make.height.equalTo(contentHeight)
        }

        UIApplication.shared.keyWindow!.addSubview(self)
        self.frame = UIScreen.main.bounds
        self.bgView.snp.updateConstraints { (make) in
            make.bottom.equalTo(contentHeight)
        }
        self.layoutIfNeeded()
        bgView.roundCorners(corners: UIRectCorner(arrayLiteral: [.topLeft,.topRight]), radius: 10.0)
//        bgView.rounding(byRoundingCorners: UIRectCorner.init(arrayLiteral: [.topLeft, .topRight]), cornerRadi: 10) //顶部圆角
        UIView.animate(withDuration: 0.24, animations: {
            self.bgView.snp.updateConstraints { (make) in
                make.bottom.equalTo(0)
            }
            self.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0.5)
            self.layoutIfNeeded()
        }, completion: { (finish) in
        })
    }
    
    @objc func dismiss() {
        UIView.animate(withDuration: 0.24, animations: {
            self.bgView.snp.updateConstraints { (make) in
                make.bottom.equalTo(self.contentHeight)
            }
            self.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0)
            
            self.layoutIfNeeded()
            
        }, completion: { (finished) in
            self.removeFromSuperview()
        })
    }

    @objc func cancelBtnAntion(){
        self.dismiss()
    }
    
    @objc func onSureButtonAction() {
        self.selectContentBlock?(self.content,self.row)
        dismiss()
    }
    
    //setModel
    func selecScroll(row: NSInteger){
        pickerView.selectRow(row, inComponent: 0, animated: true)
    }
    
    
}

extension CustomPickerView: UIPickerViewDataSource,UIPickerViewDelegate{
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.dataArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        var title: String = " "
        if let label = view as? UILabel {
            label.text = title
            return label
        }

        let label = UILabel()
        label.textAlignment = .center
        label.font = UIFont.init(name: Theme.PingFangSCMedium, size: 18)
        label.text = self.dataArray[row]
        label.sizeToFit()
        return label
    }
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if self.dataArray.count > 0{
            self.content = self.dataArray[row]
            self.row = row
        }
    }
}

extension CustomPickerView: UIGestureRecognizerDelegate{
    
}
